﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.GuessTheWords2.Resources;
using Studio77.GuessTheWords2.Core;
using Studio77.GuessTheWords2.Utils;
using Microsoft.Phone.Tasks;
using System.Windows.Threading;

namespace Studio77.GuessTheWords2
{
    public partial class MainPage : PhoneApplicationPage
    {
        private static bool isShowOpenAd = false;

        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            // 用于本地化 ApplicationBar 的示例代码
            //BuildLocalizedApplicationBar();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            for (int i = 0; i < 4; i++)
            {
                try
                {
                    this.NavigationService.RemoveBackEntry();
                }
                catch
                { }
            }

            if (StageManager.Instance.IsPlaySound)
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
            }
            if (StageManager.Instance.LastStage != 0 && StageManager.Instance.LastClass != 0)
            {
                this.btnContinue.IsEnabled = true;
            }
            else
            {
                this.btnContinue.IsEnabled = false;
            }


            this.btnNewUser.Visibility = System.Windows.Visibility.Collapsed;
            if (StageManager.Instance.IsDoNewUserTask)
            {
                this.btnNewUser.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (UmengSDK.UmengAnalytics.GetOnlineParam("useGift") == "1")
            {
                this.btnNewUser.Visibility = System.Windows.Visibility.Visible;
            }

            AudioPlayHelper.PlayBackground();

            String usemokaStr = UmengSDK.UmengAnalytics.GetOnlineParam("usemoka");
            //0-关闭 1-同时开启 2-开启banner 3-开启开屏
            if ((usemokaStr == "1" || usemokaStr == "3") && !isShowOpenAd)
            {
                isShowOpenAd = true;
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += (object ss, EventArgs ee) =>
                {
                    timer.Stop();
                    string dateStr = DateTime.Now.ToString("yyyyMMdd") + (DateTime.Now.Hour < 12 ? "A" : "P");
                    if (StageManager.Instance.LastFullScreenDate != dateStr)
                    {
                        StageManager.Instance.LastFullScreenDate = dateStr;
                        NavigationService.Navigate(new Uri("/Game77SharedLib;component/MokaPage.xaml?channel=fktzhhb", UriKind.RelativeOrAbsolute));
                    }
                };
                timer.Start();

            }
        }

        private void btnSoundOn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //关闭音效
            StageManager.Instance.IsPlaySound = false;
            AudioPlayHelper.StopBackground();
            this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnSoundOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            //打开音效
            StageManager.Instance.IsPlaySound = true;
            AudioPlayHelper.PlayBackground();
            this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            //开始游戏
            this.NavigationService.Navigate(new Uri("/Pages/ClassifyPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            //继续游戏
            if (StageManager.Instance.LastStage != 0 && StageManager.Instance.LastClass != 0)
            {
                this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml", UriKind.RelativeOrAbsolute));
            }
            else
            {
                MessageBox.Show("没有保存的游戏记录");
            }
        }

        bool isOnGift = false;

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (isOnGift)
            {
                this.StoryboardHidePackage.Begin();
                isOnGift = false;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        private void btnNewUser_Click(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            isOnGift = true;
            this.StoryboardShowPackage.Begin();
        }

        private void btnGetMB_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
            StageManager.Instance.IsCommented = true;
            StageManager.Instance.IsDoNewUserTask = true;

            //增加50
            StageManager.Instance.LampCount += 50;

            this.btnGetMB.Content = "已领取";
            this.btnGetMB.IsEnabled = false;
            this.btnNewUser.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnCloseGift_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            isOnGift = false;
            this.StoryboardHidePackage.Begin();
        }

        // 用于生成本地化 ApplicationBar 的示例代码
        //private void BuildLocalizedApplicationBar()
        //{
        //    // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
        //    ApplicationBar = new ApplicationBar();

        //    // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // 使用 AppResources 中的本地化字符串创建新菜单项。
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}